<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="660px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="产品名称" prop="product_name">
                    <el-input v-model="formData.product_name" placeholder="请输入产品名称" :maxlength="100" />
                </el-form-item>
                <el-form-item label="产品价格" prop="product_price">
                    <el-input-number style="width: 100%;" :min="0" controls-position="right" :precision="2" v-model="formData.product_price" />
                </el-form-item>
                <el-form-item label="产品图片" prop="product_picture">
                    <div>
                        <div>
                            <material-picker v-model="formData.product_picture" :limit="1" />
                        </div>
                        <div class="form-tips">建议尺寸：240*180px</div>
                    </div>
                </el-form-item>

                <el-form-item label="销售政策" prop="sales_policy">
                    <el-input type="textarea" v-model="formData.sales_policy" placeholder="请输入产品销售政策" />
                </el-form-item>
                <el-form-item label="详情" prop="details">
                    <editor v-model="formData.details" :height="400" :width="500" />
                </el-form-item>

                <el-form-item label="产品状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'

import { porductAdd, porductDetail, porductEdit } from '@/api/porduct'
import Popup from '@/components/popup/index.vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑产品' : '新增产品'
})
const formData = reactive({
    id: '',
    product_name: '',
    product_price: '',
    product_picture: '',
    sales_policy: '',
    details:'',
    status: 1
})

const formRules = {
    product_name: [
        {
            required: true,
            message: '请输入产品名称',
            trigger: ['blur']
        }
    ],
    product_price: [
        {
            required: true,
            message: '请输入产品价格',
            trigger: ['blur']
        }
    ],
    product_picture: [
        {
            required: true,
            message: '请上传图片',
            trigger: ['blur']
        }
    ],
    sales_policy: [
        {
            required: true,
            message: '请输入产品销售政策',
            trigger: ['blur']
        }
    ],
    details: [
        {
            required: true,
            message: '请输入产品详情',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await porductEdit(formData) : await porductAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await porductDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
